$(function(){
    var data_catalog =  $("#data_catalog");
    var data_particulars =  $("#data_particulars");
    var data_dialog = $("#data_dialog");
    var data_form = $('#data_form');

    //data_catalog 字典目录表初始化
    data_catalog.datagrid({
        url:'/dataDict/queryByParentIdIsNull',
        striped:"true",
        singleSelect:"true",
        fit:"true",
        fitColumns:"true",
        //在用户点击一行的时候触发事件
        onDblClickRow:function(index, row){
            var catalog = data_particulars.datagrid("options");
            catalog.url="/dataDict/queryByParentIdChildren?parentId="+row.id;
            data_particulars.datagrid("load");
        },
        columns:[[
            {field:'x',checkbox:true},
            {field:'sn',title:'字典编码',width:100},
            {field:'name',title:'字典名称',width:100},
            {field:'brief',title:'字典简介',width:100}
        ]]
    })
    //data_catalog 字典目录表初始化-------end


    //data_particulars 字典目录明细表初始化
    data_particulars.datagrid({
        striped:"true",
        singleSelect:"true",
        fit:"true",
        fitColumns:"true",
        toolbar: '#data_toolbar',
        //在用户点击一行的时候触发事件
        onClickRow:function(index, row){
            if(row.state==1){
                $("#change_btn").linkbutton({
                    text:"离职"
                })
            }else if(row.state==0){
                $("#change_btn").linkbutton({
                    text:"复职"
                })

            }
        },
        columns:[[
            {field:'x',checkbox:true},
            {field:'sn',title:'字典明细编码',width:100},
            {field:'name',title:'字典明细名称',width:100},
            {field:'brief',title:'字典明细简介',width:100},
            {field:'parent',title:'字典明细目录',width:100,
                formatter: function(value,row,index){
                        return value.name;
                }
            }
        ]]
    })
    //data_particulars 字典目录明细表初始化-------end

    //保存/编辑对话框 初始化
    data_dialog.dialog({
        width: 300,
        height: 350,
        closed: false,
        closed:true,
        buttons:"#dialog_buttons"
    });
    //保存/编辑对话框 初始化 ----end

    var cmdObj = {
        reload:function(){
            data_particulars.datagrid("reload")
        },
        add:function(){
            //清除form 表单 缓存数据
            data_form.form('clear');

            var rows = data_particulars.datagrid("getRows");
            var row = {};
            if (rows.length>0){
                //同名匹配  回显问题
                if (rows[0].parent){
                    row["parent.id"]=rows[0].parent.id;
                }
            }

            //把数据加载到 对话框的表单中
            data_form.form('load',row);


            data_dialog.dialog("setTitle","数据字典保存");
            data_dialog.dialog("open");
        },
        save:function(){
            var url ="/dataDict/save";
            var id = $("[name='id']").val();
            if(id){
                url ="/dataDict/update";
            }
            data_form.form('submit', {
                url:url,
                success:function(data){
                    data = $.parseJSON(data);
                    if(data.success){
                        data_dialog.dialog("close");
                        data_particulars.datagrid("reload");
                    }else{
                        $.messager.alert('温馨提示',data.msg);
                    }
                }
            });

        },
        edit:function(){

            //判断是否选中
            var row = data_particulars.datagrid("getSelected")
            if(row == null){
                $.messager.alert('温馨提示',"请选中需要编辑的数据字典");
                return ;
            }
            //清空 旧数据
            data_form.form('clear');

            //同名匹配  回显问题
            if (row.parent){
                row["parent.id"]=row.parent.id;
            }

            //把数据加载到 对话框的表单中
            data_form.form('load',row);

            data_dialog.dialog("setTitle","数据字典编辑");
            data_dialog.dialog("open");
        },
        remove:function(){
            //判断是否选中
            var row = data_particulars.datagrid("getSelected")
            if(row == null){
                $.messager.alert('温馨提示',"请选中需要删除的数据字典");
                return ;
            }

            $.get("/dataDict/delete?id="+row.id,function(data){
                if (data.success){
                    data_particulars.datagrid("reload")
                }else{
                    $.messager.alert('温馨提示',data.msg);
                }

            })
        },
        cancel:function(){
            data_dialog.dialog("close")
        }

    }

    //代码优化
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

})